﻿Imports WeatherServices
Imports System.Xml
Imports System.IO

Public Class Locations
    Public wxlocs As WxLocationCollection

    Private Sub Locations_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing

    End Sub

    Private Sub Locations_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False

        Lbox1.Items.Clear()

        If File.Exists(Application.StartupPath & "\locations.xml") Then
            wxlocs = LoadLocations(Application.StartupPath & "\locations.xml")

            For Each Loc As WxLocation In wxlocs
                Lbox1.Items.Add(Loc.City)
            Next
        End If
    End Sub

    Public Sub SaveLocation(ByVal path As String, ByVal wxloc As WxLocation)
        Dim doc As New XmlDocument

        doc.Load(path)

        Dim exists As Boolean

        For Each nod As XmlNode In doc.SelectNodes("/locations/location")
            If nod.SelectSingleNode("parentId").InnerText.Contains(wxloc.LocationID) Then
                exists = True
            End If
        Next

        If exists = True Then
            MsgBox("Location allready exists!")
            Exit Sub
        Else
            Dim loc As XmlElement = doc.CreateElement("location")
            Dim city As XmlElement = doc.CreateElement("city")
            Dim country As XmlElement = doc.CreateElement("country")
            Dim lat As XmlElement = doc.CreateElement("latitude")
            Dim lng As XmlElement = doc.CreateElement("longitude")
            Dim woeid As XmlElement = doc.CreateElement("parentId")
            Dim state As XmlElement = doc.CreateElement("state")
            Dim statename As XmlElement = doc.CreateElement("statename")
            Dim sel As XmlElement = doc.CreateElement("selected")
        End If
    End Sub

    Public Function LoadLocations(ByVal path As String) As WxLocationCollection
        Dim result As New WxLocationCollection()

        Dim doc As New XmlDocument

        doc.Load(path)

        For Each lc As XmlNode In doc.SelectNodes("/locations/location")
            Dim city As String = Nothing
            Dim country As String = Nothing
            Dim lat As String = Nothing
            Dim lng As String = Nothing
            Dim woeid As String = Nothing
            Dim state As String = Nothing
            Dim statename As String = Nothing
            Dim sel As String = Nothing

            city = lc.SelectSingleNode("city").InnerText
            country = lc.SelectSingleNode("country").InnerText
            lat = lc.SelectSingleNode("latitude").InnerText
            lng = lc.SelectSingleNode("longitude").InnerText
            woeid = lc.SelectSingleNode("parentId").InnerText
            state = lc.SelectSingleNode("state").InnerText
            statename = lc.SelectSingleNode("statename").InnerText
            sel = lc.SelectSingleNode("selected").InnerText

            result.Add(New WxLocation(city, state, statename, country, woeid, lat, lng, sel))
        Next

        Return result
    End Function

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btn_add.Click
        searchlocation.ShowDialog()
    End Sub

    Private Sub btn_remove_Click(sender As Object, e As EventArgs) Handles btn_remove.Click

    End Sub

    Public Sub ChangeSetting(ByVal path As String, ByVal wxloc As WxLocation)
        Dim doc As New XmlDocument

        doc.Load(path)

        For Each nod As XmlNode In doc.SelectNodes("/locations/location")
            If nod.SelectSingleNode("selected").InnerText = "true" Then
                nod.SelectSingleNode("selected").InnerText = "false"
            End If
            If nod.SelectSingleNode("parentId").InnerText = wxloc.LocationID Then
                nod.SelectSingleNode("selected").InnerText = "true"
            End If
        Next

        doc.Save(path)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btn_makecurrent.Click
        ChangeSetting(Application.StartupPath & "\locations.xml", wxlocs(Lbox1.SelectedIndex))
        Form1.curloc = wxlocs(Lbox1.SelectedIndex)
        Form1.ReloadService()
        Form1.UpdateWeather()
    End Sub

    Private Sub btn_apply_Click(sender As Object, e As EventArgs) Handles btn_apply.Click
        Me.Dispose()
        Me.Close()
    End Sub
End Class